"use client"
import React, { useEffect, useState } from "react";
import {
    Button,
    Col, Drawer,
    Form,
    Input,
    message,
    Modal,
    Row,
    Select,
    Space,
    Switch,
    Table,
    TableProps,
    Tooltip,
} from 'antd';
import renderIcons from "@/components/CustomIconSelector/conponents";
import { finishedList } from "@/clientApi/flowable/taskFinish";
import FinishTaskDetailView from "@/components/ProcessDetail";
import useResponsiveLayout from "@/utils/useResponsiveLayout";
import RenderIconsCustom from "@/components/CustomIconSelector/conponents";
import { useConfirmModal } from "@/utils/ConfirmModal";

const taskCompletedView: React.FC = () => {
        const { confirm } = useConfirmModal();
    const initialQueryParams = {
        pageNum: 1,
        pageSize: 10,
        name: undefined,

    };

    interface DataType {
    }

    const [form] = Form.useForm();
    const [record, setRecord] = useState<Record<string, any>>({});
    const [forceUpdateKey, setForceUpdateKey] = useState(0); // 用于强制更新组件的 key
    const [dataList, setDataList] = useState<DataType[]>([]);
    const [procesOpen, setProcessOpen] = useState(false);//打开详情
    const [processDetail, setProcessDetail] = useState<Record<string, any>>({});
    const columns: TableProps<DataType>['columns'] = [
        {
            title: '任务编号',
            dataIndex: 'procInsId',
            key: 'procInsId',
            align: 'center'
        },
        {
            title: '流程名称',
            dataIndex: 'procDefName',
            key: 'procDefName',
            align: 'center'
        },
        {
            title: '任务节点',
            dataIndex: 'taskName',
            key: 'taskName',
            align: 'center'
        }, {
            title: '流程类别',
            dataIndex: 'category',
            key: 'category',
            align: 'center'
        }, {
            title: '版本',
            dataIndex: 'procDefVersion',
            key: 'procDefVersion',
            align: 'center'
        }, {
            title: '业务主键',
            dataIndex: 'businessKey',
            key: 'businessKey',
            align: 'center'
        }, {
            title: '流程发起人',
            dataIndex: 'startUserName',
            key: 'startUserName',
            align: 'center',
            render: (_, record: any) => (
                <div>{record.startUserName}{record.startDeptName}</div>
            )
        }, {
            title: '接收时间',
            dataIndex: 'createTime',
            key: 'createTime',
            align: 'center'
        }, {
            title: '操作',
            dataIndex: 'action',
            key: 'action',
            render: (_, record: any) => (
                <Space size="middle">
                    {record?.roleId !== 1 && (
                        <Tooltip title="详情">
                            <Button
                                type="link"
                                size="small"
                                icon={<RenderIconsCustom icon="edit"/>}
                                onClick={() => DetailClick(record)} />
                        </Tooltip>

                    )}
                    {/*{record?.roleId !== 1 && (*/}
                    {/*    <Tooltip title="撤回">*/}
                    {/*        <Button*/}
                    {/*            type="link"*/}
                    {/*            size="small"*/}
                    {/*            icon={<DeleteOutlined/>}*/}
                    {/*            onClick={() => handleRevoke(record)}/>*/}
                    {/*    </Tooltip>*/}
                    {/*)}*/}

                </Space>
            ),
        },
    ];
    const [queryParams, setQueryParams] = useState(initialQueryParams);
    const [total, setTotal] = useState(0);
    const [openDataPermissions, setOpenDataPermissions] = useState<boolean>(false);
    useEffect(() => {
        getList();
    }, [queryParams]);  // 监听 queryParams 变化，调用 getList

    /** 查询用户列表 */
    function getList() {
        finishedList(queryParams).then((res: any) => {
            setDataList(res.rows.map((item: any) => ({
                ...item,
                // key: item.id // 使用 userId 作为唯一的 key
            })))
            setTotal(res.total); // 更新 total，用于分页
        });
    }

    // 查询列表
    function handleSearch() {
        form.validateFields().then(values => {
            console.log(values, 'values')
            setQueryParams(prevQueryParams => ({
                ...queryParams,
                name: values.name,
            }));
        });
    }

    // 重置查询
    const resetSearch = () => {
        form.resetFields();
        setQueryParams(initialQueryParams); // 重置为初始状态
    }

    // 分页变化
    const handleTableChange = (page: number, pageSize: number) => {
        setQueryParams({
            ...queryParams,
            pageNum: page,
            pageSize: pageSize,
        });
    };
    // 查看详情按钮
    const DetailClick = (e: any) => {
        setProcessDetail(e)
        setProcessOpen(true);

    }
    // 撤回按钮
    const handleRevoke = (e: any) => {
        const procInsIds = e.procInsId;
        confirm({
            title: '系统提示',
            content: (`是否确认删除用户编号为"${procInsIds}"的数据项？`),
            onOk() {
                // 用户点击“确定”按钮后执行的操作
                const params = {
                    instanceId: procInsIds
                }
                // revokeProcess(params).then((res: any) => {
                //     if (res?.code === 200) {
                //          globalMessage().info("删除成功")
                //         getList()
                //     }
                // })
                // 在这里可以添加删除记录的逻辑
            },
            onCancel() {
                // 用户点击“取消”按钮后执行的操作

            },
        });
    }
    return (
        <div className='home'>
            <Row gutter={20} style={{ paddingTop: "20px" }}>
                <Col span={24}>
                    <div><Form
                        form={form}
                        name="definition_form_controls"
                        style={{ marginTop: 10, marginLeft: 10, marginBottom: 10 }}
                       layout={useResponsiveLayout("inline")}>
                        <Form.Item
                            name="name"
                            label="流程名称">
                            <Input />
                        </Form.Item>
                        <Form.Item>
                            <Button type="primary" htmlType="submit" onClick={handleSearch} style={{
                                marginRight: 20,
                            }}>
                                查询
                            </Button>
                            <Button type="primary" htmlType="reset" onClick={resetSearch}>
                                重置
                            </Button>
                        </Form.Item>
                    </Form></div>
                    <Table<DataType> rowKey="taskId"
                        columns={columns}
                        dataSource={dataList}
                        scroll={{x:'max-content'}}
                        pagination={{
                            current: queryParams.pageNum,
                            pageSize: queryParams.pageSize,
                            total: total,
                            showSizeChanger: true, // 显示分页尺寸切换器
                            pageSizeOptions: ['10', '20', '50'], // 分页尺寸选项
                            onShowSizeChange: handleTableChange, // 分页尺寸变化时的回调
                            onChange: handleTableChange
                        }} />
                </Col>
            </Row>
            <Drawer
                title="流程详情"
                placement="right"
                closable={false}
                onClose={() => {
                    setProcessOpen(false)
                }}
                open={procesOpen}
                width={"50%"}
                key={"right"}
            >
                <FinishTaskDetailView processDetail={processDetail} />
            </Drawer>
        </div>
    )
};
export default taskCompletedView;
